/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package work.hutool.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import work.entity.Student;
import lombok.var;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Sakura Shen
 *
 * BeanUtil中的bean指的是实体对象
 *
 * @date 2021/8/20
 */
public class BeanUtilTest {

    public static void main(String[] args) {

        // 1 bean 转成 map
        Student student = new Student("小沈","22");
        var map = BeanUtil.beanToMap(student);
        System.out.println(map);
        var map1 =  BeanUtil.beanToMap(student,false,true);
        System.out.println(map1);

        // 2 map 转 bean
        Map<String,String> map2 = new HashMap<>();
        map2.put("name","小巴");
        map2.put("age","11");
        Student student1 =  BeanUtil.toBean(map2, Student.class);
        System.out.println(student1);
        Student student2 =  BeanUtil.fillBeanWithMapIgnoreCase(map2, new Student(),false);

        // 3 map转bean 设置别名
        HashMap<String, Object> map11 = new HashMap<>();
        map11.put("a_name", "Joe");
        map11.put("b_age", 12);
        // 设置别名，用于对应bean的字段名 mapping是别名映射关系的map对象
        HashMap<String, String> mapping = new HashMap<>();
        mapping.put("a_name", "name");
        mapping.put("b_age", "age");
        Person person = BeanUtil.toBean(map11, Person.class, CopyOptions.create().setFieldMapping(mapping));

        // 4 实现bean的拷贝
        Student student3 = new Student("小沈","22");
        // 浅拷贝 实际是指针的拷贝 它的变化会引起原对象（实际内存) 的变化
        Student student_copy1 = student3;
        student_copy1.setAge("99");
        // 深拷贝
        Student student_copy = new Student();
        BeanUtil.copyProperties(student3,student_copy);
        System.out.println("AAA");




    }
}
